#!/bin/sh
mkdir -p /tmp/cwmp

platform="$(cat /etc/platform)"
case "$platform" in
  GFMS*|GFHD*|GFRG*|GFMN*) board=gfmedia ;;
  GFLT*|GFCH*) board=gfonu ;;
  *) board="$platform" ;;
esac

# A previous run of cwmpd might have left this running.
# run-isostream knows how to kill its children (isostream itself) when
# terminated politely.
pkillwait run-isostream

RESTRICT="--restrict-acs-hosts=.gfsvc.com,.google.com"
if [ -e /config/CWMPD_NO_RESTRICT ]; then
  RESTRICT=
fi

CWMP="
  /usr/catawampus/cwmpd
      --platform=$board
      --ca-certs=/etc/ssl/certs/ca-certificates.crt
      $RESTRICT
"

if [ -s /tmp/ssl/certs/device.pem ] && [ -s /tmp/ssl/private/device.key ]; then
  CWMP="$CWMP --client-cert=/etc/ssl/certs/device.pem"
  CWMP="$CWMP --client-key=/etc/ssl/private/device.key"
fi

exec $CWMP "$@"
